package com.example.endproject;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class HomeActivity extends Activity {
    private static final String TAG = "HomeActivity";
    String userName;//用户名
    String ID,name,age,department,hobby,constellation,signature;//个人信息
    TextView subjectView,IDView,nameView,ageView,departmentView,hobbyView,constellationView,signatureView;//控件
    DBHelper dbHelper;
    SQLiteDatabase mSqlDB;
    String temp_ID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //初始化控件
        subjectView = findViewById(R.id.edit_subject);
        subjectView.setTypeface(Typeface.createFromAsset(getAssets(),"font/FZBangSXJW.ttf"));
        IDView = findViewById(R.id.edit_ID);
        nameView = findViewById(R.id.edit_Name);
        ageView = findViewById(R.id.edit_Age);
        departmentView = findViewById(R.id.edit_Department);
        hobbyView = findViewById(R.id.edit_Hobby);
        constellationView = findViewById(R.id.edit_Constellation);
        signatureView = findViewById(R.id.edit_Signature);
        //获取用户名
        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        dbHelper = new DBHelper(this);
        mSqlDB = dbHelper.getWritableDatabase();

        //查询关系表Relate中的ID
        Cursor query1 = mSqlDB.query(DBHelper.TB_NAME_Relate, new String[]{"user_name", "ID"},
                "user_name = ?", new String[]{userName}, null, null, null);
        if(!query1.moveToFirst()){
            Log.i(TAG, "onCreate: 根据传递的user_name在关系表中找不到对应的ID");
            finish();
            return;
        }
        temp_ID = ID = query1.getString(query1.getColumnIndexOrThrow("ID"));
        query1.close();
        Log.i(TAG, "onCreate: ID="+ID);
        //根据ID查对应信息
        Cursor query2 = mSqlDB.query(DBHelper.TB_NAME_MSG,
                new String[]{"ID", "name", "age", "department", "hobby", "constellation", "signature"}
                , "ID = ?", new String[]{ID}, null, null, null);
        if(!query2.moveToFirst()){
            Log.i(TAG, "onCreate: 根据关系表中的ID在信息表找不到对应信息");
            finish();
            return;
        }
        //赋值
        name = query2.getString(query2.getColumnIndexOrThrow("name"));
        age = query2.getString(query2.getColumnIndexOrThrow("age"));
        department = query2.getString(query2.getColumnIndexOrThrow("department"));
        hobby = query2.getString(query2.getColumnIndexOrThrow("hobby"));
        constellation = query2.getString(query2.getColumnIndexOrThrow("constellation"));
        signature = query2.getString(query2.getColumnIndexOrThrow("signature"));
        query2.close();

        subjectView.setText("您好,"+name);
        IDView.setText(ID);
        nameView.setText(name);
        ageView.setText(age);
        departmentView.setText(department);
        hobbyView.setText(hobby);
        constellationView.setText(constellation);
        signatureView.setText(signature);
    }

    public void onclick_save(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("保存")
                .setMessage("是否保存当前改动")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ID = IDView.getText().toString();
                        name = nameView.getText().toString();
                        age = ageView.getText().toString();
                        department = departmentView.getText().toString();
                        hobby = hobbyView.getText().toString();
                        constellation = constellationView.getText().toString();
                        signature = signatureView.getText().toString();
                        Log.i(TAG, "onclick_save: mmm");
                        ContentValues values = new ContentValues();
                        values.put("ID",ID);
                        values.put("name",name);
                        values.put("age",age);
                        values.put("department",department);
                        values.put("hobby",hobby);
                        values.put("constellation",constellation);
                        values.put("signature",signature);
                        ContentValues values1 = new ContentValues();
                        values1.put("ID",ID);
                        try{
                            mSqlDB.beginTransaction();
                            mSqlDB.update(DBHelper.TB_NAME_MSG,values,"ID = ?",new String[]{temp_ID});
                            temp_ID = ID;//这个赋值保证了不管点击几次保存按钮都会存入对应的值
                            mSqlDB.update(DBHelper.TB_NAME_Relate,values1,"user_name = ?",new String[]{userName});
                            Log.i(TAG, "onclick_save: ID= "+ID);
                            mSqlDB.setTransactionSuccessful();
                        }catch (SQLException e){
                            IDView.setText("");
                            IDView.setHint("请输入正确的学号");
                        }
                        finally {
                            mSqlDB.endTransaction();
                        }
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        IDView.setText(ID);
                        nameView.setText(name);
                        ageView.setText(age);
                        departmentView.setText(department);
                        hobbyView.setText(hobby);
                        constellationView.setText(constellation);
                        signatureView.setText(signature);
                    }
                });
        builder.create().show();



    }
}